package com.eip.zd.controller;

import com.eip.zd.common.JwtTokenUtils;
import com.eip.zd.common.Result;
import com.eip.zd.entity.User;
import com.eip.zd.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        String token = userService.login(user.getUserId(), user.getPassword());
        return Result.success(token);
    }
    @GetMapping("/getUserById")
    public Result getUserById(@RequestParam String userId) {
//        QueryWrapper<User> wrapper = new QueryWrapper<>();
//        wrapper.eq("id", id);
        return Result.success(userService.getUserById(userId));
    }
    @GetMapping("/getCurUser")
    public Result getCurUser() {
        User current = JwtTokenUtils.getCurrent();
        return Result.success(current);
    }
    @GetMapping("/getUserList")
    public Result getUserList(@RequestParam(required = false) String keyword) {
        List<User> userList = userService.getUserList(keyword);
        return Result.success(userList);
    }
    @PostMapping("/add")
    public Result addUser(@RequestBody User user) {
        userService.add(user);
        return Result.success();
    }
    @PostMapping("/update")
    public Result updateUser(@RequestBody User user) {
        userService.update(user);
        return Result.success();
    }
    @PostMapping("/remove/{id}")
    public Result removeUser(@PathVariable Long id) {
        userService.remove(id);
        return Result.success();
    }
}
